package org.crackjack.dao;

import java.sql.SQLException;
import java.util.List;

import org.crackjack.model.Profile;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

@Repository
public class ProfileDao extends GenericDao<Profile> {

    private JdbcTemplate jdbcTemplate;

    public boolean authorization(String login, String password) {
        return checkUserExists(login, password);
    }

    public boolean registration(String login, String password) {
        boolean isUserExists = checkUserExists(login, password);
        if (isUserExists) {
            return false;
        } else {
            jdbcTemplate
                    .update("insert into profiles(login, password) values(?, ?) ", new Object[] { login, password });
            return true;
        }
    }

    private boolean checkUserExists(String login, String password) {
        return jdbcTemplate.queryForInt("SELECT COUNT(*) FROM profiles WHERE login='" + login + "' AND password='"
                + password + "'") == 1 ? true : false;
    }

    @Autowired
    public void setJdbcTemaplate(JdbcTemplate jdbcTemaplate) {
        this.jdbcTemplate = jdbcTemaplate;
    }

    @Override
    public Profile get(final Long id) {
        return hibernateTemplate.execute(new HibernateCallback<Profile>() {

            @Override
            public Profile doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria criteria = session.createCriteria(Profile.class);
                criteria.add(Property.forName("id").eq(id));
                return (Profile) criteria.uniqueResult();
            }
        });
    }

    @Override
    public void delete(final Long id) {
        hibernateTemplate.execute(new HibernateCallback<Boolean>() {

            @Override
            public Boolean doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria criteria = session.createCriteria(Profile.class);
                criteria.add(Property.forName("id").eq(id));
                if (criteria.uniqueResult() != null) {
                    session.delete(criteria.uniqueResult());
                    return true;
                } else {
                    return false;
                }
            }
        });
    }

    @Override
    public List<Profile> getList() {

        return hibernateTemplate.execute(new HibernateCallback<List<Profile>>() {

            @Override
            public List<Profile> doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria criteria = session.createCriteria(Profile.class);
                return criteria.list();
            }
        });
    }

}
